<?php

namespace App\Http\Controllers\Api;

use App\Models\goods;
use App\Models\goods_attr_val;
use App\Models\goods_image;
use App\Models\goods_sku;
use App\Models\order_info;
use App\Models\reservation;
use App\Models\user;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ReservationController extends Controller
{
    public $pageSize = 10;
    /**
     * 商品详情
     */
    public function addReservation(Request $request)
    {
        try {
            $openid = $request->openid;
            $address_id = $request->json('address_id');
            $time = $request->json('time');
            $type = $request->json('type');
            $estimated_weight = $request->json('estimated_weight');
            if(!$address_id || !$time || !$type || !$estimated_weight) fun_respon(0, '缺少其他的信息哦！');
            if(!in_array($type,[1,2,3])) fun_respon(0, '类型错误！');
            if(time() >= strtotime($time)){
                fun_respon(0, '请选择稍晚一点的时间哦！');
            }
            $order_no = time().rand(1000,9999).rand(1000, 9999);
            $arr['address_id'] = $address_id;
            $arr['order_no'] = $order_no;
            $arr['time'] = $time;
            $arr['type'] = $type;
            $arr['estimated_weight'] = $estimated_weight;
            $arr['openid'] = $openid;
            $res = reservation::create($arr);
            if(!$res){
                fun_respon(0, '添加失败！');
            }
            fun_respon(1, '添加成功！');
        } catch (\Exception $ex) {
            fun_respon(0, $ex->getMessage().'系统繁忙，请稍后再试');
        }
    }


    /**
     * @param Request $request
     * 获取预约列表
     */
    public function getReservationList(Request $request){
        $openid = $request->openid;
        $user_info = user::where(['openid'=>$openid])->first();
        if($user_info['identity']==2){
            $where = [];
        }else{
            $where = ['reservation.openid'=>$openid];
        }
        $list = reservation::leftjoin('address','reservation.address_id','=','address.id')->where($where)->get(['reservation.id','type','status','time','username','phone','address','created_at','estimated_weight','total_bonus']);
        if($list){
            foreach($list as &$value){
                $value['created_at'] = date('Y-m-d',strtotime($value['created_at']));
                $value['time'] = date('Y-m-d H:i',strtotime($value['time']));
            }
            fun_respon(1, $list);
        }
        fun_respon(0, '暂无数据！');
    }

    /**
     * @param Request $request
     * 获取兑换订单详情
     */
    public function getReservationDetail(Request $request){
        $res_id = $request->json('res_id');
        if(!$res_id){
            fun_respon(0, '缺少信息！');
        }
        $openid = $request->openid;
        /*if($openid!='onZZo5DpHaxlEfDbNPoNG9rtZw7w' || $openid!='onZZo5MYYfPC7LdoX1Z1psu3p0kM'){
            fun_respon(0, '暂无权限操作！');
        }*/
        $info = reservation::leftjoin('address','reservation.address_id','=','address.id')->where(['reservation.id'=>$res_id])->first(['reservation.id','type','status','time','username','phone','address','created_at','estimated_weight','order_no','total_bonus']);
        if($info){
            $info['created_at'] = date('Y-m-d',strtotime($info['created_at']));
            $info['time'] = date('Y-m-d H:i',strtotime($info['time']));
            fun_respon(1, $info);
        }
        fun_respon(0, '暂无数据！');
    }

    /**
     * @param Request $request
     * 修改预约订单的状态
     */
    public function editReservationStatus(Request $request){
        $openid = $request->openid;
        $user_info= user::where(['openid'=>$openid])->first();
        if(!$user_info){
            fun_respon(0, '系统繁忙，请稍后再试');
        }

        $res_id = $request->json('res_id');
        $estimated_weight = $request->json('estimated_weight',0);
        $arr = ['onZZo5DpHaxlEfDbNPoNG9rtZw7w','onZZo5MYYfPC7LdoX1Z1psu3p0kM'];
        if(!in_array($openid,$arr)){
            fun_respon(0, '暂无权限操作！');
        }
        $res_info = reservation::where(['id'=>$res_id])->first();
        if(!$res_info){
            fun_respon(0, '暂无该订单！');
        }

        $data = ['status'=>2];
        $where = ['id'=>$res_id,'status'=>1];
        if($estimated_weight){
            $data['estimated_weight'] = $estimated_weight;
        }else{
            $data['estimated_weight'] = $res_info->estimated_weight;
        }
        //
        if($res_info['type']==1){
            $data['total_bonus'] = $data['estimated_weight']*5;
        }elseif ($res_info['type']==2){
            $data['total_bonus'] = $data['estimated_weight']*5;
        }else{
            $data['total_bonus'] = $data['estimated_weight']*0.5;
        }

        $res = reservation::where($where)->update($data);
        if(!$res){
            fun_respon(0, '修改失败！');
        }

        $before_bonus = $user_info['bonus'];
        $change_bonus = $data['estimated_weight'];
        $after_bonus = $before_bonus+$change_bonus;
        $status = 3;
        $type = 1;
        //更新用户的积分和皇冠
        //$resss = user::where(['openid'=>$openid])->update(['bonus'=>$after_bonus]);
        //$record_bonus = sprintf("兑换,扣除积分%s", $change_bonus);
        //积分扣除
        add_bonus_record($openid,$before_bonus,$change_bonus,$after_bonus,$status,$type,0);

        fun_respon(1, '修改成功！');
    }




    private function getIsBuy($goods_id,$user_id){
        $res = order_info::where(['user_id'=>$user_id,'order_status'=>2])->first();
        return $res?1:0;
    }

    /**
     * 商品列表
     */
    public function goodsList(Request $request)
    {
        try {
            $openid = $request->openid;
            $page = $request->json('page', 1);
            $offset = $this->pageSize*($page-1);
            $where = [];
            $data = goods::where($where)->take($this->pageSize)->skip($offset)->select('id','title','retail_price','discount_price','original_price','goods_cover','goods_type','description')->get();
            if($data){
                foreach ($data as &$v){
                    //查询订单的商品信息
                    //$v['goods_sku'] = $this->getSku($v['id']);
                    //查询是否购买过该商品
                    $v['is_buy'] = $this->getIsBuy($v['id'],$openid);
                }
            }

//            $type = $request->json('type', 1);
//            $goods_id = $request->json('goods_id', 176);
//            $goods_info = goods::where(['id'=>$goods_id])->first();
//            //var_dump($goods_info);exit;
//            $num = $type==1?1:50;
//            $current_price = $type==1?'19.8':'12.8';
//            $info['goods_id'] = 1;
//            $info['goods_name'] = '鹰嘴脆-全球首款鹰嘴脆零食';
//            $info['original_price'] = '29.8';
//            $info['current_price'] = $current_price;
//            $info['banner_image'] = [
//                'https://bengong-1256861713.cos.ap-beijing.myqcloud.com/shop/shop_detail_banner0.png',
//                'https://bengong-1256861713.cos.ap-beijing.myqcloud.com/shop/shop_detail_banner1.jpg',
//                'https://bengong-1256861713.cos.ap-beijing.myqcloud.com/shop/shop_detail_banner2.jpg',
//                'https://bengong-1256861713.cos.ap-beijing.myqcloud.com/shop/shop_detail_banner3.jpg',
//                'https://bengong-1256861713.cos.ap-beijing.myqcloud.com/shop/shop_detail_banner4.jpg',
//            ];
//            $info['goods_sku'] = [
//                ['id'=>1,'name'=>'炭烧香辣味','number'=>$num],
//                ['id'=>2,'name'=>'芥末海苔味','number'=>$num],
//                ['id'=>3,'name'=>'枣夹核桃味','number'=>$num],
//                ['id'=>4,'name'=>'开心坚果味','number'=>$num],
//            ];
//            $info['detail_image'] = '';
            $datas['goods_list'] = $data;
            $datas['banner'] = [];
            fun_respon(1, $datas);
        } catch (\Exception $ex) {
            fun_respon(0, $ex->getMessage().'系统繁忙，请稍后再试');
        }
    }



}
